/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Portions created by the Initial Developer are
 * Copyright (C) 2012 the Initial Developer.
 * All Rights Reserved.
 *
 * The Original Code is "http://semi-frame.googlecode.com/svn/trenk/".
 *
 * The Initial Developer of the Original Code is the author below.
 */
/**
 * @file TaskManager.cpp
 * @brief auto generated.
 * @author mornongcloud
 * @date 2011/07/21
 */

#include "semi/TaskManager.h"

namespace semi {

TaskManager::TaskManager() {
}

TaskManager::~TaskManager() {
}

// static
bool TaskManager::execute(std::string taskid) {

	CurrentTasks mCrnt(taskid);

	if (mCrnt.isValid())
		return false; // already execute
	//	mCrnt.setValid();

	// fork
	int pid = 0;
	if ((pid = fork()) == 0) {

		conf::TasksConfig::TaskConfig tconf = conf::TasksConfig().getConfig(
				taskid);
		const char* path = tconf.task_path;
		const char* args = tconf.task_args;

		std::cout << "  ##TaskManager::execute path:" << path << std::endl;
		pid = execlp(path, path, args, 0);
		exit(0);
	}

	return true;
}

// static
void TaskManager::executeAll() {

	conf::TasksConfig::TaskConfigs tconfs = conf::TasksConfig::getConfigs();

	for (int idx = 0; idx < (sizeof(conf::TasksConfig::TaskConfigs)
			/ sizeof(conf::TasksConfig::TaskConfig)); idx++) {

		conf::TasksConfig::TaskConfig cnf = tconfs.taskConfig[idx];

		CurrentTasks mCrnt(std::string(cnf.task_id));
		if (mCrnt.isValid())
			continue; // already execute
		//		mCrnt.setValid();

		const char* path = cnf.task_path;
		const char* args = cnf.task_args;

		// fork
		int pid = 0;
		if ((pid = fork()) == 0) {
			pid = execlp(path, path, args, 0);
			std::cout << "  ##TaskManager::execute path:" << path << std::endl;
			exit(0);
		}
	}
	return;
}

}
